#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,urllib,re,md5,os
import MySQLdb
import ConfigParser
import htmllib
import formatter
import time
import thread
import re
import urllib2
import datetime
class getUserInfo():
    def __init__(self):
        self.space = "http://space.360quan.com/u/"
        self.beginTime = time.time()
    
    def format_html(self,str):
        str = str.replace("/", "")
        str = str.replace("\\", "")
        str = str.replace(",", "，")
        str = str.replace("'","‘")
        str = str.replace("\"","“")
        return str
    
    def getUserInfo(self,infoU):
        url = self.space + infoU[1]
        html = self.getHtml(url)
        uid = html.split("var host_id = \"")[1].split("\";")[0].strip()
        nickname = html.split("\" : \"")[1].split("\";")[0].strip()
        nickname = self.format_html(nickname)
        gender   = html.split("<div class=\"information\">")[1].split("<iframe  name=\"custom_iframe\"")[0].split("性别：")[1].split("<br />")[0].strip()
        birthday = html.split("calculateYearOfAstro('")[1].split("'));</script>)")[0].strip()
        try:
            reside = html.split("<div class=\"information\">")[1].split("<iframe  name=\"custom_iframe\"")[0].split("现居：")[1].split("<br />")[0].strip()
        except:
            reside = ""
        
        try:
            birthplace = html.split("<div class=\"information\">")[1].split("<iframe  name=\"custom_iframe\"")[0].split("故乡：")[1].split("<br />")[0].strip()
        except:
            birthplace = ""
        
        try:
            recommend = html.split("<div class=\"information\">")[1].split("<iframe  name=\"custom_iframe\"")[0].split("自我介绍：<br />")[1].split("</div>")[0].strip()
        except:
            recommend = ""
        recommend = self.format_html(recommend)
        space = html.split("var host_seq = \"")[1].split("\";")[0].replace("u/","").strip()
        sql = "insert into user_info (username,nickname,pic,gender,birthday,reside,birthplace,recommend,temp_uid,space) values ('"+nickname+"','"+nickname+"','"+infoU[2]+"','"+gender+"','"+birthday+"','"+reside+"','"+birthplace+"','"+recommend+"',"+uid+",'"+space+"')"
        db = self.dbConn()
        cursor = db.cursor()
        cursor.execute("SET NAMES 'utf8'")
        cursor.execute(sql)
        db.close()
        self.up(space)
    
    def up(self,space):
        u_space = space.replace("u/","")
        sql = "update user_task set status=-2 where u_space='"+u_space+"'"
        print sql
        db = self.dbConn()
        cursor = db.cursor()
        cursor.execute(sql)
        db.close()
    
    def run(self):
        while True:
            spaceList = self.se()
            if len(spaceList) < 1:
                break
            for u in spaceList:
                spaceUrl = u[1]
                print spaceUrl
                self.getUserInfo(u)
                print "当前",time.time() - self.beginTime
    
    def se(self):
      limitSql = "select * from user_task where status=0 limit 10"
      db = self.dbConn()
      cursor = db.cursor()
      cursor.execute(limitSql)
      info = cursor.fetchall()
      db.close()
      return info
    
    #get file code
    def getHtml(self,domain):
        try:
            wp = urllib.urlopen(domain)
        except:
            print 'url exception'
        content = wp.read()
        wp.close()
        return content
    
    #get config file
    def initConfig(self,configFile):
        config = ConfigParser.ConfigParser();
        config.readfp(open(configFile));
        return config;
    
    def matchCode(self,regular,content):
        str = re.compile(regular,re.I|re.S)
        return str.findall(content)
    
    def dbConn(self,dbServer="default"):
        config = self.initConfig("../config/_db.ini")
        user = str(config.get(dbServer,"user"))
        db   = str(config.get(dbServer,"db"))
        passwd   = str(config.get(dbServer,"passwd"))
        host   = str(config.get(dbServer,"host"))
        conn = MySQLdb.connect(user=user, db=db, passwd=passwd, host=host)
        return conn
    
if __name__=='__main__':
    u = getUserInfo()
    u.run()